package com.shop.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shop.dao.IAdmin;
import com.shop.dao.impl.AdminDao;
import com.shop.model.Admin;


@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public LoginServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String username = request.getParameter("username");		
		String password = request.getParameter("password"); 
		
		System.out.println(username+":"+password);
		AdminDao admin = new AdminDao();
		Integer ad = admin.checkExists(username, password);
		if (ad  > 0) {
//			request.setAttribute("account", username);
			request.getSession().setAttribute("account", username);
			System.out.println("账户存在，可以成功登陆");
//			request.getRequestDispatcher("index.jsp").forward(request, response);
			response.getWriter().append("index.jsp");
		}else {
//			response.sendRedirect("error.jsp");
			response.getWriter().append("error.jsp");
		}
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
